package com.hegongda.servlet;

import com.hegongda.bean.*;
import com.hegongda.service.ProductService;
import com.hegongda.utils.BeanFactory;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(value = "/productServlet")
public class ProductServlet extends BaseServlet{
    private ProductService ps = BeanFactory.newInstance(ProductService.class);
    public void getAllNav(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String admin = request.getParameter("admin");
        List<Nav> allNav = ps.getAllNav();
        if(admin != null){
            request.setAttribute("nav",allNav);
            request.getRequestDispatcher("/view/admin/category/list.jsp").forward(request,response);
        }else {
            Result re = new Result(Result.SUCCESS, "查询成功");
            re.setObj(allNav);
            response.getWriter().print(JSONObject.fromObject(re));
        }
    }

    public void getHostAndNew(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Product> host = ps.getHost();
        List<Product> news = ps.getNews();
        Map<String,List<Product>> list = new HashMap<String,List<Product>>();
        list.put("news",news);
        list.put("host",host);
        Result re = new Result(Result.SUCCESS,"查询成功");
        re.setObj(list);
        response.getWriter().print(JSONObject.fromObject(re));
    }


    public void getOneProduct(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String pid = request.getParameter("pid");
        Product oneProduct = ps.getOneProduct(pid);
        Introduce oneProductI = ps.getOneProductI(oneProduct.getIid());
        request.setAttribute("op",oneProduct);
        request.setAttribute("opi",oneProductI);
        request.getRequestDispatcher("/view/product/info.jsp").forward(request,response);
    }

    public void getPageProduct(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String cid = request.getParameter("cid");
        String pagenum = request.getParameter("pagenum");
        PageBean<Product> pageProduct = ps.getPageProduct(cid,Constr.PAGESIZE,Integer.valueOf(pagenum));
        request.setAttribute("pb",pageProduct);
        request.setAttribute("cid",cid);
        request.getRequestDispatcher("/view/product/list.jsp").forward(request,response);
    }

    public void foundByNamePage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String search = request.getParameter("search");
        String pagenum = request.getParameter("pagenum");
        PageBean<Product> pageProduct = ps.foundByNamePage(search,Constr.PAGESIZE,Integer.valueOf(pagenum));
        Result re = new Result(Result.SUCCESS,"查询成功");
        re.setObj(pageProduct);
        response.getWriter().print(JSONObject.fromObject(re));
    }
}
